<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ComponentInviteFriend.class.php, $Author:  $ */



/** Invite friends components
 *
 * Parameters:
 *  1. oUser (mandatory) - user account object
 *
 * @package module.account
 * @version $Id: PhpFox_ComponentInviteFriend.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 *
 */
class PhpFox_Mod_Account_ComponentInviteFriend extends BaseComponent
{

    /** Processing method
     */
	function process()
	{
	    $oAcc = $this->getParam('oUser');
	    /* @var $oAcc PhpFox_Mod_Account_ItemAccount */
	    assert($oAcc != null);

	    //validator
        $aFields = array();
        $iNumEmails = App::getParam('Mod_Account.invite.num_emails');
        $aSended = array(); //sended emails array
        $aMember = array();
        $aInvite = array(); 
        for($i = 1; $i <= $iNumEmails; $i++)
        {
            $aFields['aInvite'.$i] = array('title'    => ''. App::format('Mod_Account.friends_email') .' '.$i,
                                           'def'      => 'email',
                                           'optional' => true);
		}

        $oValidator = App::newObject('Validator', $aFields);
        /* @var $oValidator Validator */

	    $oReq = &App::getRequest();
	    /* @var $oReq Request */

	    // processing
        switch($oReq->getAction())
        {
            case 'send_invite':
                if ($oValidator->isValid($oReq->getAll()))
                {
                    $aEmails = array();
                    for($i = 1; $i <= $iNumEmails; $i++)
                    {
                        if ($oReq->get('aInvite'.$i))
                        {
	                        $sEmail = $oReq->get('aInvite'.$i);
                        	
                        	$aCheck = $this->oDatabase->getRow("SELECT email FROM ". App::getT('invite') ." WHERE user = '" . App::getUser('user') . "' AND email = '". $sEmail ."'");
	                        $aEmail = $this->oDatabase->getRow("SELECT email FROM ". App::getT('user') ." WHERE email = '". $sEmail ."'");	
                    	
							if (isset($aEmail['email']))
							{								
								$aMember[] = $sEmail;	
							}
	                        else if (isset($aCheck['email']))
							{
                        		$aInvite[] = $sEmail;
							}							
							else 
							{
                        		$aEmails[] = $sEmail;
							}
                        }
                    }
                    if ($aEmails)
                    {
                        $oSrvInvite = &$this->_oModule->getService('Invite');
                        /* @var $oSrvInvite PhpFox_Mod_Account_ServiceInvite */
	                    $aSended = $oSrvInvite->sendInvites($aEmails, $oAcc, $oReq->get('text'));
                    }
                }
                else
                {
                    $this->addErrors($oValidator->getErrors());
                }
				break;
        }

        $oTpl = &$this->_oModule->getTemplate();
		$oTpl->assignSrc(array(
			'aInvites' => $aInvite,
			'aMembers' => $aMember,
			'iNumEmails'   => $iNumEmails,
			'aSended'      => $aSended,
			'jsValidation' => $oValidator->makeJS(),
			'jsValMess'    => $oValidator->makeJsMess(),
			'aErrors'      => $this->getErrors(),
		));
		$srcHtml = $oTpl->fetch('InviteFriend.html');
		return $srcHtml;
	}


}
?>